import { observable, action, toJS } from 'mobx';
import _ from "lodash";
import { message, Modal } from 'antd';
// import window from '../../util';
import {createContract, getCompanys, getServers} from "../../apis/fileSignApi";
class FileSignStore {
    @observable loading = true;
    @observable params;
    @observable signType = '1';
    @observable companyname = '';
    @observable companyvisible = false;
    @observable selectedFile = "";
    @observable selectedFileData = {};
    @observable refresh = 0;
    @observable companys = [];
    @observable servers = [];
    @observable serverName = '';
    @observable serverId = '';

    @action
    init = () => {

        getServers({}).then((result) => {
            if(result.status == "1" && result.servers && result.servers.length > 0) {
                this.servers = result.servers;
                this.servers.map(server => {
                    if(server.selected) {
                        this.serverName = server.serverName;
                        this.serverId = server.serverId;
                    }
                });
                this.getCompanys();
            }
        })

    }

    getCompanys = () => {
        getCompanys({serverId: this.serverId}).then((result) => {
            if (result.status == "1"&&result.data&&result.data.length>0) {
                this.companys=result.data;
                this.companys.map(com=>{
                    if(com.selected){
                        this.companyname=com.showname;
                    }
                })
                if(this.companyname==''){
                    this.companyname=this.companys[0].showname;
                }
            }
        })
    }

    getOptions = () =>{
        let options = new Array();
        this.companys.map(com=>{
            options.push({key:com.key,showname:com.showname,selected:com.selected});
        })
        return options;
    }

    getServerOptions = () => {
        let options = new Array();
        this.servers.map(server => {
            options.push({key:server.serverId, showname:server.serverName, selected:server.selected})
        });
        return options;
    }



    @action
    updateParams = (params) => {
        let temp = !_.isEqual(toJS(this.params), params);
        this.params = params;
        if (temp) {
            this.refresh += 1;
        }
    }

    @action
    changeSignType = (value) => {
        this.signType = value;
        if(value=='2'){
            this.companyvisible=true;
        }else{
            this.companyvisible=false;
        }
    }

    @action
    getShowListDatas = (__, newData) => {
        if (newData && newData.length == 1) {
            this.selectedFile = _.get(newData, '[0].name');
            this.selectedFileData = { filename: _.get(newData, '[0].name'), fileid: "-999", size: _.get(newData, '[0].size') };
        } else {
            this.selectedFile = "";
        }
    }

    @action
    deleteFile = () => {
        const that = this;
        Modal.confirm({
            title: '系统提示',
            content: '你确认删除该待上传文件吗？',
            onOk() {
                that.selectedFile = "";
                that.selectedFileData = {};
                that.refresh += 1;
            },
            onCancel() {
            },
        });
    }

    @action
    toSign = () => {
        if(this.serverName == '') {
            message.error("契约锁服务不能为空");
            return;
        }
        if(this.signType=='2'&&this.companyname==''){
            message.error("企业名称不能为空!");
            return;
        }
        window.startUploadAll();
    }

    @action
    uploadChange = (ids, list) => {
        if (ids && ids.length == 1) {
            let excelid = ids[0];
            let filename=list[0].filename;
            let fileExtendName = list[0].fileExtendName;
            filename=filename.replace(/\..*/g,'');
            const params = {
                fieldid: excelid,
                filename:filename,
                signtype:this.signType,
                companyname:this.companyname,
                fileExtendName: fileExtendName,
                serverId: this.serverId
            }
            //附件上传成功去签章
            createContract(params).then((data) => {
                if (data.status == "1"&&data.url) {
                    window.openFullWindowHaveBar(data.url);
                } else {
                    Modal.error({
                        title: "创建失败",
                        width: 500,
                        content: <div style={{ maxHeight: 600, overflowY: 'auto' }}><div dangerouslySetInnerHTML={{ __html: "创建签章文件发生错误" }}></div></div>,
                        onOk() { },
                    });
                }
            })
        } else {
            message.error("文件上传失败！");
        }
    }

    @action
     setServerId = (serverId) => {
        this.serverId = serverId;
    }

}
export default FileSignStore;